package io.renren.modules.bs.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.dto.PatientDto;
import io.renren.modules.bs.entity.BsDepartmentPatientEntity;
import io.renren.modules.bs.service.BsDepartmentPatientService;
import io.renren.modules.bs.service.BsPatientService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-15 23:03:15
 */
@RestController
@RequestMapping("bs/departmentPatient")
public class BsDepartmentPatientController {
    @Autowired
    private BsDepartmentPatientService bsDepartmentPatientService;

    @Autowired
    private BsPatientService patientService;

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String,Object> params){
        PageUtils page = bsDepartmentPatientService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{departmentPatientId}")

    public R info(@PathVariable("departmentPatientId") Long departmentPatientId){
		BsDepartmentPatientEntity bsDepartmentPatient = bsDepartmentPatientService.getById(departmentPatientId);

        return R.ok().put("bsDepartmentPatient", bsDepartmentPatient);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    private R save( @RequestBody PatientDto patientDto){

        String idNumber = patientDto.getIdNumber();
        if(StringUtils.isNotBlank(idNumber)){
            Integer num = this.patientService.countByIdNumber(idNumber);
            if(num == 0){
                this.patientService.savePatient(patientDto);
            }else {
                throw new RRException("身份证号在住院信息里已存在");
            }
        }else {
            throw new RRException("身份证号不能为空");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BsDepartmentPatientEntity bsDepartmentPatient){
		bsDepartmentPatientService.updateById(bsDepartmentPatient);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Long[] departmentPatientIds){
		bsDepartmentPatientService.removeModelByIds(Arrays.asList(departmentPatientIds));
        return R.ok();
    }


    /**
     * 导出
     */
    @RequestMapping("/export")

    public void export(@RequestParam Map<String,Object> params, HttpServletResponse response){
        this.bsDepartmentPatientService.export(params,response);
    }

}
